package com.kefu.admin.modules.tgqmdj.controller;

import com.kefu.admin.modules.tgqmdj.model.Menu;
import com.kefu.admin.modules.tgqmdj.model.TreeMenu;
import com.kefu.admin.modules.tgqmdj.model.TreeMenuList;
import com.kefu.admin.modules.tgqmdj.service.impl.MenuServiceImpl;
import com.kefu.admin.utils.CoreConst;
import com.kefu.admin.utils.Ret;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
/**
 * @Description:
 * @version: v1.0.0
 * @author Hjj
 * @date: 2019年3月11日 下午4:36:17
 */
@Controller
@RequestMapping("/tgqmdj")
public class MenuController{
    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);
    /* 1:全部资源，2：菜单资源*/
    private static final String[] MENU_FLAG ={"1","2"};
    @Autowired
    private MenuServiceImpl menuServiceImpl;

 
    @RequestMapping(value={"/menulist"})
    public String menulist(HttpServletRequest request){
        return "tgqmdj/menulist";
    }

    /*权限列表数据*/
    @RequestMapping(value={"/list"})
    @ResponseBody
    public List<Menu>  loadMenu(String flag){
        List<Menu> MenutList = new ArrayList<Menu>();
        if(StringUtils.isBlank(flag) || MENU_FLAG[0].equals(flag)){
        	MenutList = menuServiceImpl.queryMenu(CoreConst.STATUS_VALID);
        }else if(MENU_FLAG[1].equals(flag)){
        	MenutList = menuServiceImpl.queryMenuName(CoreConst.STATUS_VALID);
        }
        return MenutList;
    }
    /*TreeMenu数据*/
    @RequestMapping(value={"/treelist"})
    @ResponseBody
    public List<TreeMenu>  treeMenu(String flag){
        List<TreeMenu> MenutList = menuServiceImpl.queryTreeMenu();
       
        return MenutList;
    }
    /*新增菜单*/
    @RequestMapping(value={"/insertMenu"})
    @ResponseBody
    public Ret insertMenu(Menu menu){
    	int index = menuServiceImpl.insertMenu(menu);
    	if(index<0){
    		return Ret.error();
    	}
        return Ret.ok().put("code", 1);
    }
    
    /*根据Id查询菜单*/
    @RequestMapping(value={"/queryMenuById"})
    @ResponseBody
    public Ret queryMenuById(Long menuId){
    	Menu menu = menuServiceImpl.queryMenuById(menuId);
    	if(menu == null){
    		return Ret.error();
    	}
        return Ret.ok().put("data", menu);
    }
    /*修改菜单*/
    @RequestMapping(value={"/updataMenuById"})
    @ResponseBody
    public Ret updataMenuById(Menu menu){
    	int index = menuServiceImpl.updataMenuById(menu);
    	if(index<0){
    		return Ret.error();
    	}
        return Ret.ok().put("code", 1);
    }
    
    /*删除菜单*/
    @RequestMapping(value={"/deleteMenuById"})
    @ResponseBody
    public Ret deleteMenuById(Long menuId){
    	int index = menuServiceImpl.deleteMenuById(menuId);
    	if(index<0){
    		return Ret.error();
    	}
        return Ret.ok().put("code", 1);
    }
    
	/**
	 * 
	 * 动态菜单查询
	 * @param menu 
	 * @return
	 */
	@RequestMapping(value={"/selectMenu"})
	@ResponseBody
	public Ret selectMenu(){
		//user.setUserId(getUserId());
		List<TreeMenuList> menulist = menuServiceImpl.selectMenu();
		if(menulist==null){
			return Ret.error("数据为空");
		}
		return Ret.ok().put("data", menulist);
	}
}
